<?php
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
	
	$lang = $_SESSION['lang'];
	if (empty($lang) || $lang == "EN") {
		include "../value/value_en.php";
	} else if($lang == "VN") {
		include "../value/value_vn.php";
	}
	else {
		include "../value/value_kr.php";
	}

include '../content-accountant/invoicetax.php';
$stonggiacungcap=0;
$stongthue=0;
$stongtien=0;
$arr=array();
$time="";

$status=$_REQUEST['status'];
if($status==0){
	$l_ngaygiaodich=$l_ngaygiaodich0;
	$l_ngayphathanh=$l_ngayphathanh0;	
	$l_thoigiangiaodich=$l_thoigiangiaodich0;
}
if($status==1){
	$l_ngaygiaodich=$l_ngaygiaodich1;
	$l_ngayphathanh=$l_ngayphathanh1;	
	$l_thoigiangiaodich=$l_thoigiangiaodich1;
}
if(isset($_SESSION['manginvoicetax'])){
	$arr=$_SESSION['manginvoicetax'];
}
if(isset($_REQUEST['df']) && isset($_REQUEST['dt'])){
	$time= $_REQUEST['df']." ~ ".$_REQUEST['dt'];
}
$addressbrowser = $_SERVER['SERVER_NAME'];
$temp = explode(".", $addressbrowser);

$typedate=$_REQUEST['typedate'];
if($typedate=='ngayph'){
	$type=$l_phathanh;
}
if($typedate=='ngaygd'){
	$type=$l_giaodich;
}

if (isset($_SESSION['manginvoicetax'])) {
	
	//include '../Classes/PHPExcel.php';
	/** Include path **/
	set_include_path(get_include_path() . PATH_SEPARATOR . 'ClassesExcel/');
	
	/** PHPExcel */
	require_once 'PHPExcel.php';
	
	/** PHPExcel_RichText */
	require_once 'PHPExcel/RichText.php';
	
	
	$filename ="export_acc.xls";
	
	$objPHPExcel = new PHPExcel();

	
	$styleArray = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,
			'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$styleArray1 = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray2 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray3 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray4 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray5 = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'font' => array(
			'bold' => true,
			
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	/*$objPHPExcel->getProperties()->setCreator('Matthew Casperson')
	->setLastModifiedBy('Matthew Casperson')
	->setTitle('Office 2007 XLSX Test Document')
	->setSubject('Office 2007 XLSX Test Document')
	->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.');
	
	
	$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(8); */
	
	$objPHPExcel->setActiveSheetIndex(0);
	
	$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Times New Roman');
	$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->mergeCells('A1:I1');
	$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->setCellValue('A1',$l_hoadonthue);
	
	$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->mergeCells('A2:C2');
	$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray3);
	$objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray($styleArray3);
	$objPHPExcel->getActiveSheet()->getStyle('C2')->applyFromArray($styleArray3);
	$objPHPExcel->getActiveSheet()->setCellValue('A2',$l_company_name.' : '.strtoupper($temp[0]));
	
	if($_REQUEST['cusname']!=""){
		$objPHPExcel->getActiveSheet()->mergeCells('G2:I2');
		$objPHPExcel->getActiveSheet()->getStyle('G2')->applyFromArray($styleArray4);
		$objPHPExcel->getActiveSheet()->getStyle('H2')->applyFromArray($styleArray4);
		$objPHPExcel->getActiveSheet()->getStyle('I2')->applyFromArray($styleArray4);
		$objPHPExcel->getActiveSheet()->setCellValue('G2',$l_tenkhachhanggiaodich.' : '.$_REQUEST['cusname']);
	}
	
	if($time!=""){
		$objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(25);
		$objPHPExcel->getActiveSheet()->mergeCells('A3:C3');
		$objPHPExcel->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->getStyle('B3')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->getStyle('C3')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->setCellValue('A3',$l_thoigiangiaodich.' : '.$time);
	}
	
	$objPHPExcel->getActiveSheet()->freezePane('A5');
	
	$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('A4',$l_no);
	
	$objPHPExcel->getActiveSheet()->getStyle('B4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
	$objPHPExcel->getActiveSheet()->setCellValue('B4', $l_ngaythangnam);
	
	$objPHPExcel->getActiveSheet()->getStyle('C4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('C4', $l_company_name);
	
	$objPHPExcel->getActiveSheet()->getStyle('D4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('D4', $l_product_item);
	
	$objPHPExcel->getActiveSheet()->getStyle('E4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('E4', $l_quantity);
	
	$objPHPExcel->getActiveSheet()->getStyle('F4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
	$objPHPExcel->getActiveSheet()->setCellValue('F4', $l_Supply_price);
	
	$objPHPExcel->getActiveSheet()->getStyle('G4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
	$objPHPExcel->getActiveSheet()->setCellValue('G4', $l_price_vat);
	
	$objPHPExcel->getActiveSheet()->getStyle('H4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
	$objPHPExcel->getActiveSheet()->setCellValue('H4', $l_amount);
	
	$objPHPExcel->getActiveSheet()->getStyle('I4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
	$objPHPExcel->getActiveSheet()->setCellValue('I4', $l_thoigiangiaodich);
	$soluong= count($arr);
	$key=5;
           if($soluong>0){
           		$vitri=1;
           		foreach($arr as $thoigian=>$mgiatri){
           			$tonggiacungcap=0;
           			$tongthue=0;
           			$tongtien=0;
           			$dem=count($mgiatri);
           			//echo "<br><br>".$dem;		
           			if($dem>0){
           				for($i=0;$i<$dem;$i++){
           					//echo "<br>".$i;
           					$inobject= unserialize (serialize ($mgiatri[$i]));
           					$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(25);
							$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('A'.$key, $vitri);
							
							$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('B'.$key, $inobject->ordertime);
							
							$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray3);
							$objPHPExcel->getActiveSheet()->setCellValue('C'.$key,$inobject->cusname);
							
							$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray3);
							$objPHPExcel->getActiveSheet()->setCellValue('D'.$key, $inobject->order_content);
							
							$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('E'.$key, $inobject->order_quantity);
							
							$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->applyFromArray($styleArray4);
							$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->getNumberFormat()->setFormatCode('#,##0');
							$objPHPExcel->getActiveSheet()->setCellValue('F'.$key, $inobject->order_suplly_rice);
							
							$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($styleArray4);
							$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->getNumberFormat()->setFormatCode('#,##0');
							$objPHPExcel->getActiveSheet()->setCellValue('G'.$key, $inobject->order_tax);
							
							$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($styleArray4);
							$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getNumberFormat()->setFormatCode('#,##0');
							$objPHPExcel->getActiveSheet()->setCellValue('H'.$key, $inobject->order_total);
							
							$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('I'.$key, $inobject->order_time_transaction);
							
							
           					
           					
           					$tonggiacungcap +=$inobject->order_suplly_rice;
		            		$tongthue +=$inobject->order_tax;
		            		$tongtien +=$inobject->order_total;
		            		$vitri++;
		            		$key++;
           				}
           			}
           			$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(25);
           			$objPHPExcel->getActiveSheet()->mergeCells('A'.$key.':D'.$key);
					$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray2);
					$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray2);
					$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray2);
					$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray2);
					
					if($_REQUEST['type']=="ngay"){
						$objPHPExcel->getActiveSheet()->setCellValue('A'.$key,$l_days.': '.$thoigian.' - '.$type.':'.number_format($dem));
					}else{
						$objPHPExcel->getActiveSheet()->setCellValue('A'.$key,$l_month.': '.$thoigian.' - '.$type.':'.number_format($dem));
					}
					
					
					

					$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray2);
					$objPHPExcel->getActiveSheet()->setCellValue('E'.$key,'');
					
					$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->applyFromArray($styleArray5);
					$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->getNumberFormat()->setFormatCode('#,##0');
					$objPHPExcel->getActiveSheet()->setCellValue('F'.$key, $tonggiacungcap);
					
					$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($styleArray5);
					$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->getNumberFormat()->setFormatCode('#,##0');
					$objPHPExcel->getActiveSheet()->setCellValue('G'.$key, $tongthue);
					
					$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($styleArray5);
					$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getNumberFormat()->setFormatCode('#,##0');
					$objPHPExcel->getActiveSheet()->setCellValue('H'.$key, $tongtien);
					
					$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($styleArray2);
					$objPHPExcel->getActiveSheet()->setCellValue('I'.$key,'');
           			
           			$key++;
           			
           			$stonggiacungcap +=$tonggiacungcap;
			        $stongthue +=$tongthue;
			        $stongtien +=$tongtien;
			        
           		}
           		
           		$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(30);
           		$objPHPExcel->getActiveSheet()->mergeCells('A'.$key.':D'.$key);
				$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->setCellValue('A'.$key,$l_tongdon);

				$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->setCellValue('E'.$key,'');
				
				$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->applyFromArray($styleArray5);
				$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->getNumberFormat()->setFormatCode('#,##0');
				$objPHPExcel->getActiveSheet()->setCellValue('F'.$key, $stonggiacungcap);
				
				$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($styleArray5);
				$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->getNumberFormat()->setFormatCode('#,##0');
				$objPHPExcel->getActiveSheet()->setCellValue('G'.$key, $stongthue);
				
				$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($styleArray5);
				$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getNumberFormat()->setFormatCode('#,##0');
				$objPHPExcel->getActiveSheet()->setCellValue('H'.$key, $stongtien);
				
				$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($styleArray2);
				$objPHPExcel->getActiveSheet()->setCellValue('I'.$key,'');
           }
	
	include 'PHPExcel/IOFactory.php';
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Content-type: application/vnd.ms-excel");
	header('Content-Disposition: attachment;filename="' . $filename);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
}
exit;

?>

